package com.litree.service.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.litree.service.model.LoginBean;
import com.litree.service.services.ICommonSerivce;
import com.litree.service.utils.CommonUtils;
import com.litree.service.utils.Crypt;
import com.litree.service.utils.ReturnMSGUtils;
import com.litree.service.utils.ReturnMsg;
import com.litree.service.vo.LocationVo;


/**
 * 
 * @author GM
 *
 */
@SuppressWarnings({ "rawtypes" })
@Controller
public class CityAndAreaController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(CityAndAreaController.class);

	@Autowired
	private ICommonSerivce service;
/**
 * 地名查询接口
 * @param searchKey
 * @param response
 */
	@RequestMapping(method=RequestMethod.GET, value="/common/areaInfo/{searchKey}/{pageSize}/{pageNo}")
	public void getAreaInfo(@PathVariable String searchKey,@PathVariable int pageSize,@PathVariable int pageNo,HttpServletResponse response,HttpServletRequest request ) {
		try{			
			String[] pararms = {searchKey};
			if(!CommonUtils.checkParam(pararms,"inputNormalParam")){
				ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.ILLEGAL_PARAM_ERROR, ReturnMsg.ILLEGAL_PARAM_ERROR_DESC+",请重新输入!");
			}else{
				if(pageSize==0){
					ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.ILLEGAL_PARAM_ERROR,ReturnMsg.ILLEGAL_PARAM_ERROR+",每页条数不能为空");
				}else if(pageNo==0){
					ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.ILLEGAL_PARAM_ERROR,ReturnMsg.ILLEGAL_PARAM_ERROR+",每码不能为空");
				}else{
					Map tokenMap = (Map) request.getAttribute("tokenMap");
					List dataList = service.searchAraeInfo(searchKey,pageSize,pageNo);
					Long totalCount = service.searchAraeInfoCount(searchKey);
					String loginName = (String) tokenMap.get("loginName");
					saveLoginSearchKey(loginName,searchKey);
					if(!dataList.isEmpty()&&dataList.size()>0){
						ReturnMSGUtils.sentMsg(response, dataList.size(), ReturnMsg.OPTION_SUCCESS, ReturnMsg.OPTION_SUCCESS_DESC,"areaList",dataList,"totalresult",totalCount);
					}else{
						ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.NO_SELECTED_RESLUTS,ReturnMsg.NO_SELECTED_RESLUTS_DESC);
					}
				}
			}
		}catch(Exception e){
			try {
				ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e.toString());
				logger.error(CommonUtils.errMsg(ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 保存最近查询内容
	 * @param loginName
	 * @param searchKey
	 */
private void saveLoginSearchKey(String loginName, String searchKey) {
	try{
		service.setLoginSearchKey(loginName,searchKey);
	}catch(Exception e){
		logger.error(CommonUtils.errMsg(ReturnMsg.OPTION_ERROR, ReturnMsg.OPTION_ERROR_DESC+" 保存最近查询内容失败", e));
	}
	
}

/**
 * 水站查询接口 通过省份
 * @param provinceName
 * @param pageSize
 * @param pageNo
 * @param response
 * @param request
 */
		@RequestMapping(method=RequestMethod.POST, value="/common/stationInfo",consumes = "application/json")
		public void getStationInfo(@RequestBody String bodyStr,HttpServletResponse response,HttpServletRequest request ) {
			//{pageSize:5,pageNo:1,provinceCode:'469000',cityCode:'',districtCode:'',stationAddress:'',stationName:''}
			try{	
				LocationVo location = JSON.parseObject(bodyStr, LocationVo.class);
				if(location.getPageSize()==0){
					ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.PARAM_ERROR, ReturnMsg.PARAM_ERROR_DESC+",每页条数不能为空!");
				}else if(location.getPageNo()==0){
					ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.PARAM_ERROR, ReturnMsg.PARAM_ERROR_DESC+",页码不能为空!");
				}else{
					String[] pararms = {location.getProvinceCode(),location.getCityCode(),location.getDistrictCode(),
							location.getStationAddress(),location.getStationName()};
					if(!CommonUtils.checkParam(pararms,"inputNormalParam")){
						ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.ILLEGAL_PARAM_ERROR, ReturnMsg.ILLEGAL_PARAM_ERROR_DESC+",请重新输入!");
					}else{
						List dataList = service.getStationInfo(location);
						Long totalCount = service.getStationInfoCount(location);
						if(!dataList.isEmpty()&&dataList.size()>0){
							ReturnMSGUtils.sentMsg(response, dataList.size(), ReturnMsg.OPTION_SUCCESS, ReturnMsg.OPTION_SUCCESS_DESC,"dataList",dataList,"totalresult",totalCount);
						}else{
							ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.NO_SELECTED_RESLUTS,ReturnMsg.NO_SELECTED_RESLUTS_DESC);
						}
					}
				}
			}catch(Exception e){
				try {
					ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e.toString());
					logger.error(CommonUtils.errMsg(ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		


		/**
		 * 查询最近 5次的查询关键字
		 * @param response
		 * @param request
		 */
		@RequestMapping(method=RequestMethod.GET, value="/common/areaInfo/lastSearchKey")
		public void getLastSearchKey(HttpServletResponse response,HttpServletRequest request ) {
			try{			
				Map tokenMap = (Map) request.getAttribute("tokenMap");
				String loginName = (String) tokenMap.get("loginName");
				List dataList = service.getLastSearchKey(loginName);
				if(!dataList.isEmpty()&&dataList.size()>0){
					ReturnMSGUtils.sentMsg(response, dataList.size(), ReturnMsg.OPTION_SUCCESS, ReturnMsg.OPTION_SUCCESS_DESC,"lastSearchKeyList",dataList);
				}else{
					ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.NO_SELECTED_RESLUTS,ReturnMsg.NO_SELECTED_RESLUTS_DESC);
				}
			}catch(Exception e){
				try {
					ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e.toString());
					logger.error(CommonUtils.errMsg(ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
}

